import {bcComp, bcDiv, isEmpty} from "../common/Helpers";
import Common from "../Common";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MyProgress extends Common {

    protected speed = 5;
    protected progress = 0
    protected width = 0
    protected currentWidth = 0

    progressBar: cc.ProgressBar

    start() {
        this.initUserInfo().then(userInfo => {
            if (isEmpty(userInfo)) {
                cc.director.loadScene('login')
            } else {
                cc.director.loadScene('hall')
            }
        })

    }

    getWidth() {
        if (!this.width) {
            this.width = this.node.width
        }
        return this.width;
    }

    getProgressBar() {
        if (!this.progressBar) {
            this.progressBar = this.node.getComponent(cc.ProgressBar)
        }
        return this.progressBar;
    }

    update(dt: number) {
        this.currentWidth += this.speed
        if (this.currentWidth >= this.getWidth()) {
            return;
        }
        let progress = bcDiv(this.currentWidth, this.getWidth())
        if (bcComp(0.9, progress) === -1) {
            progress = 0.9;
        }
        this.progress = progress
        this.getProgressBar().progress = this.progress

    }


}
